<?php

class SiteController extends HomeController {

    public function actionIndex() {
    //Home page        	
        $this->render('home');
    }
                       
    public function actionAbout() {
        $model=  StaticPage::model()->findByAttributes(array('type'=>StaticPage::TYPE_ABOUT));
        if(!$model){
            $model=new StaticPage;
        }
        
        $this->render('about', array('model'=>$model));
    }
 
    public function actionSolution() {
        $category = Category::model()->findByAttributes(array('alias' => 'giai-phap'));
        $criteria = new CDbCriteria();
        $criteria->condition = 'status=1 AND categoryId=' . $category->id;
        $criteria->order = 'create_time DESC';
        $dataProvider = new CActiveDataProvider('Page', array(
                    'pagination' => array(
                        'pageSize' => Yii::app()->params['postsPerPage'],
                    ),
                    'criteria' => $criteria,
                ));
        $this->render('solution', array(
            'dataProvider' => $dataProvider,
            'category' => $category->name
        ));
    }

    public function actionNews() {
	$model=new Topic;        
        $model->categoryId=  TopicCategory::TYPE_NEWS_ID;        
        $this->render('support', array('model'=>$model,
             'fileId' => 'Tin tức mới nhất',
             'idcategory' => TopicCategory::TYPE_NEWS_ID,
            ));
    }

    public function actionQuote() {
        $criteria = new CDbCriteria();
        $criteria->condition = 'fileId=' . File::QUOTE;
        $dataProvider = new CActiveDataProvider('File', array(
                    'pagination' => array(
                        'pageSize' => Yii::app()->params['postsPerPage'],
                    ),
                    'criteria' => $criteria,
                ));
        $this->render('file', array(
            'dataProvider' => $dataProvider,
            'fileId' => 'Báo giá',
        ));
    }

    public function actionDocument() {
        $criteria = new CDbCriteria();
        $criteria->condition = 'fileId=' . File::DS;
        $dataProvider = new CActiveDataProvider('File', array(
                    'pagination' => array(
                        'pageSize' => Yii::app()->params['postsPerPage'],
                    ),
                    'criteria' => $criteria,
                ));
        $this->render('file', array(
            'dataProvider' => $dataProvider,
            'fileId' => 'Tài liệu - Phần mềm',
        ));
    }

    public function actionPage() {
        $category = Category::model()->findByAttributes(array('alias' => $_GET['alias']));
        if ($category->alias == 'gioi-thieu') {
            $about = Page::model()->findByAttributes(array('categoryId' => $category->id));
            $this->render('category', array(
                'model' => $about
            ));
        } elseif ($category->alias == 'giai-phap' or $category->alias == 'tin-tuc') {
            $criteria = new CDbCriteria();
            $criteria->condition = 'status=1 AND categoryId=' . $category->id;
            $criteria->order = 'create_time DESC';
            $dataProvider = new CActiveDataProvider('Page', array(
                        'pagination' => array(
                            'pageSize' => Yii::app()->params['postsPerPage'],
                        ),
                        'criteria' => $criteria,
                    ));
            $this->render('page', array(
                'name' => $category->name,
                'dataProvider' => $dataProvider,
            ));
        }
    }
 
    public function actionSearch() {

	$key = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $this->pageTitle = 'Tim kiem: ' . $key;
        if (!empty($key)) {
            $criteria = new CDbCriteria();
            $criteria->condition = 'name like "%' . $key . '%"';
            $criteria->order = 'id DESC';
            $dataProvider = new CActiveDataProvider('Product', array('pagination' => array(
                            'pageSize' => 50,
                        ),
                        'criteria' => $criteria,
                    ));
            $this->render('search', array('dataProvider' => $dataProvider));
        } else {
            throw new CHttpException(400, 'Ban phai nhap tu khoa tim kiem');
        }
    }

    public function actionContact() {

        $page=  StaticPage::model()->findByAttributes(array('type'=>StaticPage::TYPE_CONTACT));
        iF(!$page){
            $page=new StaticPage;
        }
        
        
	$model = new Contact;
        if (isset($_POST['Contact'])) {
            $model->attributes = $_POST['Contact'];
            $model->upload=  CUploadedFile::getInstance($model, 'upload');
            if ($model->validate()) {
                /*
                Yii::import('application.extensions.phpmailer.JPhpMailer');
                $mail = new JPhpMailer;
                //$mail->IsSMTP();
                $mail->Host = 'smpt.gmail.com';
                $mail->Port = 465;
                $mail->SMTPAuth = true;
                $mail->Username = 'huynhtuvinh87@gmail.com';
                $mail->Password = '';
                $mail->SetFrom($model->email, $model->name);
                $mail->Subject = $model->subject;
                $mail->AltBody = $model->body;
                $mail->MsgHTML('<h1>' . $model->subject . '</h1><br>' . $model->body);
                $mail->AddAddress('huynhtuvinh87@gmail.com', 'David Vinh');
                $mail->Send();
                CVarDumper::dump($model, 10, true);
                exit;
                Yii::app()->user->setFlash('contact', 'Cảm on khách hàng đã liên hệ với chúng tôi.');
                $this->refresh();*/
                $model->save(false);
                Yii::app()->user->setFlash('contact', 'Cảm on khách hàng đã liên hệ với chúng tôi.');
                $model->unsetAttributes();
            }
        }
        $this->render('contact', array('page'=>$page, 'model' => $model));
    }
    
    public function actionSupport(){
        $model=new Topic;
        $model->categoryId=  TopicCategory::TYPE_SUPPORT_ID;        
        $this->render('support', array('model'=>$model,
                'fileId' => 'Hỗ trợ',
                 'idcategory' => TopicCategory::TYPE_SUPPORT_ID,
                ));
    }
    
    public function actionSupportDetail($id){
        
        $model=  Topic::model()->findByAttributes(array('id'=>$id));        
        if(!$model){
            throw new CHttpException(404);
        }        
        $this->render('support_detail', array('data'=>$model));
    }

}

